package epam.bookShelf.bookStuff;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import epam.bookShelf.stuff.Library;

public class Book {
    private long    id;
    private String  title;
    private long    isbn;
    private long    dat;
    private long    readedDate;
    private String  description;
    private long    borrowRecordId;
    private String  authorName;
    private long    locationId;
    private String  readerLogin;

    private ArrayList<Library> libraries;           

    Book(String title) {
        this.title = title;
        libraries = new ArrayList<Library>();
    }
   
    public String toString() {
        StringBuilder rez = new StringBuilder("BOOK ");
        rez.append("ID" + ":  " + id + "\n");
        rez.append("TITLE" + ":  " + title);
                
        return rez.toString();
    }

    protected Element createNode(
    		Element root, 
    		Document doc, 
    		String name, 
    		String value) 
    {
        Element child = doc.createElement(name);
        root.appendChild(child);

        Text text = doc.createTextNode(value);
        child.appendChild(text);

        return child;
    }
    
    public Element getDOMElement(Document doc) {
    	Element root = doc.createElement("book");

    	//TITLE
    	root.appendChild(createNode(root, doc, "title", title));

    	//ISBN
    	if (isbn >= 0) {
    		root.appendChild(createNode(root, doc, "isbn", 
    				Long.toString(isbn)));
    	}

    	//DAT       
    	root.appendChild(createNode(root, doc, "date", 
    			Long.toString(dat)));

    	//Readed_date
    	if (getReadedDate() >= 0) {
    		root.appendChild(createNode(root, doc, "readedDate", 
    				Long.toString(readedDate)));
    	} 

    	//Description        
    	if (getDescription() != null) {
    		root.appendChild(createNode(root, doc, "description", 
    				description));
    	}

    	//Borrow_Recordid
    	if (getBorrowRecordId() >= 0) {
    		root.appendChild(createNode(root, doc, "borrowRecord", 
    				Long.toString(borrowRecordId)));
    	}


    	if (getAuthorName() != null) {
    		root.appendChild(createNode(root, doc, "author", 
    				authorName));
    	}

    	if (getLocationId() >= 0) {
    		root.appendChild(createNode(root, doc, "location", 
    				Long.toString(locationId)));
    	}

    	return root;
    }
    
    
    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((authorName == null) ? 0 : authorName.hashCode());
		result = prime * result
				+ (int) (borrowRecordId ^ (borrowRecordId >>> 32));
		result = prime * result + (int) (dat ^ (dat >>> 32));
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + (int) (isbn ^ (isbn >>> 32));
		result = prime * result
				+ ((libraries == null) ? 0 : libraries.hashCode());
		result = prime * result + (int) (locationId ^ (locationId >>> 32));
		result = prime * result + (int) (readedDate ^ (readedDate >>> 32));
		result = prime * result
				+ ((readerLogin == null) ? 0 : readerLogin.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (authorName == null) {
			if (other.authorName != null)
				return false;
		} else if (!authorName.equals(other.authorName))
			return false;
		if (borrowRecordId != other.borrowRecordId)
			return false;
		if (dat != other.dat)
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (isbn != other.isbn)
			return false;
		if (libraries == null) {
			if (other.libraries != null)
				return false;
		} else if (!libraries.equals(other.libraries))
			return false;
		if (locationId != other.locationId)
			return false;
		if (readedDate != other.readedDate)
			return false;
		if (readerLogin == null) {
			if (other.readerLogin != null)
				return false;
		} else if (!readerLogin.equals(other.readerLogin))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
        
    //=======Getters/setters area=========   
	public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getIsbn() {
        return isbn;
    }

    public void setIsbn(long isbn) {
        this.isbn = isbn;
    }

    public long getDat() {
        return dat;
    }

    public void setDat(long dat) {
        this.dat = dat;
    }

    public long getReadedDate() {
        return readedDate;
    }

    public void setReadedDate(long readedDate) {
        this.readedDate = readedDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getBorrowRecordId() {
        return borrowRecordId;
    }

    public void setBorrowRecordId(long borrowRecordId) {
        this.borrowRecordId = borrowRecordId;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public long getLocationId() {
        return locationId;
    }

    public void setLocationId(long locationId) {
        this.locationId = locationId;
    }

    public String getReaderLogin() {
        return readerLogin;
    }

    public void setReaderLogin(String readerLogin) {
        this.readerLogin = readerLogin;
    }

    public ArrayList<Library> getLibraries() {
        return libraries;
    }

    public void setLibraries(ArrayList<Library> libraries) {
        this.libraries = libraries;
    }
    
    
}